//
//  SupprotSearchTableViewCell.m
//  quxiangtao
//
//  Created by xbmios on 2018/11/6.
//  Copyright © 2018年 shanhaijin. All rights reserved.
//

#import "SupprotSearchTableViewCell.h"
#import "MarkLabel.h"

@interface SupprotSearchTableViewCell ()

// 商品图
@property (strong, nonatomic) UIImageView *goodIV;
// 券图案
//@property (strong, nonatomic) UIImageView *vouchIV;
// 券金额
//@property (strong, nonatomic) UILabel *vouchMoneyL;
// 标题
@property (strong, nonatomic) UILabel *titleL;
// 优惠价
@property (strong, nonatomic) UILabel *salePriceL;
// 原价
@property (strong, nonatomic) UILabel *originalPriceL;
// 已售
//@property (strong, nonatomic) UILabel *sellOutL;

// 券
//@property (strong, nonatomic) MarkLabel *ticketL;
// 趣豆抵扣
@property (strong, nonatomic) MarkLabel *integDiscountL;

@property (strong, nonatomic) UIView *lineV;

@end

@implementation SupprotSearchTableViewCell

- (void)awakeFromNib {
    [super awakeFromNib];
    // Initialization code
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier {
    
    if (self == [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        
        self.contentView.backgroundColor = WhiteColor;
        
        [self.contentView addSubview:self.goodIV];
        [self.contentView addSubview:self.titleL];
        [self.contentView addSubview:self.salePriceL];
        [self.contentView addSubview:self.originalPriceL];
        [self.contentView addSubview:self.integDiscountL];
        [self.contentView addSubview:self.lineV];
        
    }
    
    return self;
    
}

- (void)setSearchModel:(SearchResultModel *)searchModel {
    
    _searchModel = searchModel;
    
    self.titleL.text = searchModel.title;
    
    NSString *price = [NSString stringWithFormat:@"￥%@", searchModel.zk_final_price];
    NSMutableAttributedString *priceAttributeStr = [[NSMutableAttributedString alloc] initWithString:price];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(20) range:NSMakeRange(0, 1)];
    [priceAttributeStr addAttribute:NSFontAttributeName value:Font(30) range:NSMakeRange(1, price.length - 1)];
    self.salePriceL.attributedText = priceAttributeStr;
    
    
    NSString *oldPrice = [NSString stringWithFormat:@"￥%@", searchModel.goods_price];
    NSDictionary *attributeDic = @{NSStrikethroughStyleAttributeName:[NSNumber numberWithInteger:NSUnderlineStyleSingle]};
    NSMutableAttributedString *oldPirceAttributeStr = [[NSMutableAttributedString alloc] initWithString:oldPrice attributes:attributeDic];
    self.originalPriceL.attributedText = oldPirceAttributeStr;
    
    
    NSString *str = nil;
    
    if ([searchModel.pict_url hasPrefix:@"http://"] || [searchModel.pict_url hasPrefix:@"https://"]) {
        
        str = searchModel.pict_url;
        
    }else {
        
        str = [NSString stringWithFormat:@"http://%@",searchModel.pict_url];
    }
    
    //    [self.goodIV sd_setImageWithURL:[NSURL URLWithString:searchModel.pict_url]];
    [self.goodIV sd_setImageWithURL:[NSURL URLWithString:str]];
    
//    [self.goodIV sd_setImageWithURL:[NSURL URLWithString:searchModel.pict_url]];

//    self.integDiscountL.text = [NSString stringWithFormat:@"趣豆抵扣:%@", searchModel.dintegral];
    
    [self.integDiscountL sizeToFit];
    
}


- (void)layoutSubviews {
    
    [super layoutSubviews];
    
    
    __weak typeof(self) weakSelf = self;
    [self.goodIV mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.contentView.mas_left).with.offset(SmallMargin);
        make.centerY.mas_equalTo(weakSelf.contentView.mas_centerY);
        make.width.mas_equalTo(jisuan(180));
        make.height.mas_equalTo(jisuan(180));
        
    }];
    
    [self.salePriceL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.titleL.mas_left);
        make.bottom.mas_equalTo(weakSelf.goodIV.mas_bottom).with.offset(-jisuan(10));
    }];
    
    [self.titleL mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(weakSelf.goodIV.mas_right).with.offset(Margin);
        make.top.mas_equalTo(weakSelf.goodIV.mas_top).with.offset(jisuan(10));
        make.right.mas_equalTo(weakSelf.contentView.mas_right).with.offset(-Margin);
    }];
    
    [self.originalPriceL mas_makeConstraints:^(MASConstraintMaker *make) {
 
        make.left.mas_equalTo(weakSelf.salePriceL.mas_right);
//        make.centerY.mas_equalTo(weakSelf.salePriceL.mas_centerY);
        
        make.bottom.mas_equalTo(weakSelf.salePriceL.mas_bottom);
    }];
    
    [self.integDiscountL mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.salePriceL.mas_left); make.bottom.mas_equalTo(weakSelf.salePriceL.mas_top).with.offset(-jisuan(10));
        
    }];
    
    [self.lineV mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.left.mas_equalTo(weakSelf.contentView.mas_left);
        make.right.mas_equalTo(weakSelf.contentView.mas_right);
        make.bottom.mas_equalTo(weakSelf.contentView.mas_bottom);
        make.height.mas_equalTo(jisuan(2));
    }];
    
}

#pragma mark -LAZY
- (UIImageView *)goodIV {
    
    if (!_goodIV) {
        
        _goodIV = [[UIImageView alloc] init];
        _goodIV.contentMode = UIViewContentModeScaleAspectFill;
        _goodIV.layer.masksToBounds = YES;
        
        _goodIV.backgroundColor = Colorf2f2f2;
        
    }
    
    return _goodIV;
}

- (UILabel *)titleL {
    
    if (!_titleL) {
        
        _titleL = [[UILabel alloc] init];
        _titleL.textAlignment = NSTextAlignmentLeft;
        _titleL.textColor = Color333333;
        _titleL.font = Font(24);
        _titleL.numberOfLines = 2;
        _titleL.lineBreakMode = NSLineBreakByTruncatingTail;
    }
    
    return _titleL;
}

- (UILabel *)salePriceL {
    
    if (!_salePriceL) {
        
        _salePriceL = [[UILabel alloc] init];
        _salePriceL.textAlignment = NSTextAlignmentLeft;
        _salePriceL.textColor = Colorff0000;
        
    }
    
    return _salePriceL;
}

- (UILabel *)originalPriceL {
    
    if (!_originalPriceL) {
        
        _originalPriceL = [[UILabel alloc] init];
        _originalPriceL.textAlignment = NSTextAlignmentLeft;
        _originalPriceL.textColor = Color999999;
        _originalPriceL.font = Font(20);
        
    }
    
    return _originalPriceL;
}


- (MarkLabel *)integDiscountL {
    
    if (!_integDiscountL) {
        
        _integDiscountL = [[MarkLabel alloc] init];
        _integDiscountL.textColor = Colorff0000;
        _integDiscountL.font = Font(20);
        
//        _integDiscountL.text = @"趣豆返";
        _integDiscountL.edgeInsets = UIEdgeInsetsMake(2, 2, 2, 2);
        _integDiscountL.layer.borderWidth = jisuan(2);
        _integDiscountL.layer.borderColor = Colorff0000.CGColor;
        _integDiscountL.layer.cornerRadius = jisuan(10);
        _integDiscountL.layer.masksToBounds = YES;
        
    }
    
    return _integDiscountL;
}

- (UIView *)lineV {
    
    if (!_lineV) {
        
        _lineV = [[UIView alloc] init];
        _lineV.backgroundColor = Colordddddd;
    }
    return _lineV;
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
